package cc.ufinity.networkdiagnose.widget;

import java.text.DecimalFormat;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.util.StringUtils;
import cc.ufinity.networkdiagnose.view.R;

public class TaskResultWlan extends GeneralTestWidget {


    private LinearLayout httptaskLayout;
    private LinearLayout ftptaskLayout;
    private LinearLayout aptaskLayout;
    private LinearLayout pingtaskLayout;
    private LinearLayout webauthtaskLayout;
    private LinearLayout peaptaskLayout;
    private LinearLayout simtaskLayout;
    private LinearLayout segregatetaskLayout;
    private LinearLayout roamtaskLayout;
    private LinearLayout snrtaskLayout;
    private LinearLayout rssitaskLayout;

    private LinearLayout httptaskTitleLayout;
    private LinearLayout aptaskTitleLayout;
    private LinearLayout webauthtaskTitleLayout;
    private LinearLayout peaptaskTitleLayout;
    private LinearLayout simtaskTitleLayout;
    private LinearLayout segregatetaskTitlteLayout;
    private LinearLayout rsnrtaskTitleLayout;
    private LinearLayout roamtaskTitleLayout;
    private String cameraFlag= "";
    private boolean isShow = false;

//	private ActivityBroadcastReceiver myReceiver;
    private Activity ctx;
    private Task task;
    private Button backBtn;
    private Button btnDraw;
    private LinearLayout taskNumLayout;
    private Handler handler;
    private String fileName;
    private Button btnPic;
    private Thread thread;
    private WlanManager wlanManager;
	private boolean connected = false;
	private boolean connected2 = false;
//    private Button uploadButton;
    public TaskResultWlan(Activity context) {
        super(context);
        inflate(context, R.layout.task_result_widget_wlan, this);
        this.ctx = context;
        wlanManager = new WlanManager(ctx);
        httptaskLayout = (LinearLayout) this.findViewById(R.id.httpTaskFinishLayout_wlan);
        ftptaskLayout = (LinearLayout) this.findViewById(R.id.ftpTaskFinishLayout_wlan);
        aptaskLayout = (LinearLayout) this.findViewById(R.id.apTaskFinishLayout_wlan);
        pingtaskLayout = (LinearLayout) this.findViewById(R.id.pingTaskFinishLayout_wlan);
        webauthtaskLayout = (LinearLayout) this.findViewById(R.id.webauthTaskFinishLayout_wlan);
        peaptaskLayout = (LinearLayout) this.findViewById(R.id.peapTaskFinishLayout_wlan);
        simtaskLayout = (LinearLayout) this.findViewById(R.id.simTaskFinishLayout_wlan);
        segregatetaskLayout = (LinearLayout) this.findViewById(R.id.segregateTaskFinishLayout_wlan);
        roamtaskLayout = (LinearLayout) this.findViewById(R.id.roamTaskFinishLayout_wlan);
        snrtaskLayout = (LinearLayout) this.findViewById(R.id.snrTaskFinishLayout_wlan);
        rssitaskLayout = (LinearLayout) this.findViewById(R.id.rssiTaskFinishLayout_wlan);

        httptaskTitleLayout = (LinearLayout) this.findViewById(R.id.httpTaskLayoutTitle_wlan);
        aptaskTitleLayout = (LinearLayout) this.findViewById(R.id.apTaskLayoutTitle_wlan);
        webauthtaskTitleLayout = (LinearLayout) this.findViewById(R.id.webauthTaskLayoutTitle_wlan);
        peaptaskTitleLayout = (LinearLayout) this.findViewById(R.id.peapTaskLayoutTitle_wlan);
        simtaskTitleLayout = (LinearLayout) this.findViewById(R.id.simTaskLayoutTitle_wlan);
        segregatetaskTitlteLayout = (LinearLayout) this.findViewById(R.id.segregateTaskLayoutTitle_wlan);
        rsnrtaskTitleLayout = (LinearLayout) this.findViewById(R.id.snrTaskLayoutTitle_wlan);
        roamtaskTitleLayout = (LinearLayout) this.findViewById(R.id.roamTaskLayoutTitle_wlan);


    //    task = (Task) ctx.getIntent().getSerializableExtra(TAG_REFER_TASK); 
        task = GlobalApp.taskOther;
 
        taskNumLayout = (LinearLayout) this.findViewById(R.id.taskNumLayout_wlan);
        backBtn = (Button) this.findViewById(R.id.task_result_btnBack_wlan);
        btnDraw = (Button) this.findViewById(R.id.btn_draw_wlan);
        btnPic = (Button)this.findViewById(R.id.btnpic_wlan);
        btnDraw.setVisibility(Button.GONE);
        btnPic.setVisibility(Button.GONE);
        backBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	back_button();
            }
        });
       
        show();
    }
    
   private void show() {
	    int size = 0;  
	    float m , n;
        float speed = 0,avgSuccess=0,delay = 0,avgDelay =0,avgSpeed = 0;
        DecimalFormat df = new DecimalFormat("#0.000");
        DecimalFormat df1 = new DecimalFormat("#0.0");
        TextView t = new TextView(ctx);
        StringBuffer s = new StringBuffer();  
    	//HTTP测试 （任务测试中的巡检测试没有）
    	/*if(!task.id.equals("") && task.taskType.equals("巡检测试")) {
    		
    	} else {*/
    		httptaskTitleLayout.setVisibility(VISIBLE);
    		s.append("平均时延：").append(df.format(StringUtils.parseFloat(task.httpAnwerDelay))).append(" s").append('\n');
            s.append("访问成功率：").append(task.httpCallSuccessRate).append(" %");
            t.setTextColor(Color.WHITE);
            t.setText(s);
            httptaskLayout.addView(t);
//    	}  
    	

      //FTP测试   (都有)
        
        s = new StringBuffer();
        float tempUpAvg = Float.parseFloat(task.ftpUpAvgSuccessRate) * 8;
        float tempDownAvg = Float.parseFloat(task.ftpDownAvgSuccessRate) * 8;
        s.append("平均上传速度：").append(tempUpAvg).append(" Kb/s").append('\n');
        s.append("平均下载速度：").append(tempDownAvg).append(" Kb/s").append('\n');
        m = (float) StringUtils.parseFloat(task.ftpUpCount);
        n = (float) StringUtils.parseFloat(task.ftpUpSuccessCount);
        s.append("上传成功率：").append(m==0?0:df.format( 100*n / m)).append(" %").append('\n');
        m = (float) StringUtils.parseFloat(task.ftpDownCount);
        n = (float) StringUtils.parseFloat(task.ftpDownSuccessCount);
        s.append("下载成功率：").append(m==0?0:df.format( 100*n / m)).append(" %");
        t = new TextView(ctx);
        t.setTextColor(Color.WHITE);
        t.setText(s);
        ftptaskLayout.addView(t);
        
      //AP 关联测试 （WLAN 有）
 //       if(task.selNetType == ConnectivityManager.TYPE_WIFI) {
        	aptaskTitleLayout.setVisibility(VISIBLE);
            s = new StringBuffer();
            s.append("平均关联时间：").append(task.relevanceAPAvgTime).append("s").append('\n');
            s.append("关联成功率：").append(task.relevanceAPSuccessRate).append("%");
            t = new TextView(ctx);
            t.setTextColor(Color.WHITE);
            t.setText(s);
            aptaskLayout.addView(t);
 //       }

      //PING 测试   (都有)
        s = new StringBuffer();
        s.append("平均时延：").append(df.format((float) StringUtils.parseFloat(task.pingAvgDelay))).append(" s").append('\n');
        m = (float) StringUtils.parseFloat(task.pingCount);
        n = (float) StringUtils.parseFloat(task.pingSuccessCount);
        s.append("丢包率：").append(m==0?0:df.format( 100*(m-n) / m)).append(" %");
        t = new TextView(ctx);
        t.setTextColor(Color.WHITE);
        t.setText(s);
        pingtaskLayout.addView(t);

      //WEB 关联测试 （WLAN 有）
//        if(task.selNetType == ConnectivityManager.TYPE_WIFI) {
        	webauthtaskTitleLayout.setVisibility(VISIBLE);
            s = new StringBuffer();
            s.append("平均认证时间：").append(task.webDelay).append("s").append('\n');
            s.append("认证成功率：").append(task.webSuccessRate).append("%").append('\n');
            s.append("下线成功率：").append(task.webOffSuccessRate).append("%");
            t = new TextView(ctx);
            t.setTextColor(Color.WHITE);
            t.setText(s);
            webauthtaskLayout.addView(t);
//        } 
/*        if(task.id.equals("") && task.selNetType == ConnectivityManager.TYPE_WIFI) {
        	isShow = true;
        } else if(!task.id.equals("") && task.selNetType == ConnectivityManager.TYPE_WIFI
        		&& task.taskType.equals("验收测试")) {
        	isShow = true;
        }
        System.err.println(task.taskType+":"+task.selNetType+":"+task.testFlag);
        if(isShow) {*/
        	
        	//PEAP 测试 （一键测试WLAN、验收测试WLAN）
        	peaptaskTitleLayout.setVisibility(VISIBLE);
            s = new StringBuffer();
            s.append("当前wifi网络类型不支持Peap认证\n");
			
            t = new TextView(ctx);
            t.setTextColor(Color.WHITE);
            t.setText(s);
            peaptaskLayout.addView(t);
            
          //SIM 测试（一键测试WLAN、验收测试WLAN）
           /* simtaskTitleLayout.setVisibility(VISIBLE);
            s = new StringBuffer();
            s.append("认证成功率：").append(task.simAuthSuccessRate).append("%");
            s.append("\n下线成功率：").append(task.simAuthSuccessRate).append("%"); //====================
            t = new TextView(ctx);
            t.setTextColor(Color.WHITE);
            t.setText(s);
            simtaskLayout.addView(t);*/
            
          //segregate 测试（一键测试WLAN、验收测试WLAN）
            roamtaskTitleLayout.setVisibility(VISIBLE);
            s = new StringBuffer();
            s.append("成功率：").append(task.userIsolateSuccessRate).append("%");
            t = new TextView(ctx);
            t.setTextColor(Color.WHITE);
            t.setText(s);
            roamtaskLayout.addView(t);
            
          //roam 测试（一键测试WLAN、验收测试WLAN）
            segregatetaskTitlteLayout.setVisibility(VISIBLE);
            s = new StringBuffer();
            s.append("成功率：").append(task.userRoamSuccessRate).append("%");
            t = new TextView(ctx);
            t.setTextColor(Color.WHITE);
            t.setText(s);
            segregatetaskLayout.addView(t);
 //       }
        
/*        if(task.selNetType == ConnectivityManager.TYPE_MOBILE){
        	rsnrtaskTitleLayout.setVisibility(VISIBLE);
        	snrtaskLayout.setVisibility(VISIBLE);
        	//snr 测试   (都有)
            s = new StringBuffer();
            s.append("SNR：").append(task.snr);
            t = new TextView(ctx);
            t.setTextColor(Color.WHITE);
            t.setText(s);
            snrtaskLayout.addView(t); 
        }*/ 
            
      //rssi 测试   (都有)
        s = new StringBuffer();
//        s.append("CI：").append(task.ci);
//        s.append("\nLAC：").append(task.lac);
//        s.append("\n信号强度：").append(task.rssi).append(" dBm");
        s.append("信号强度：").append(task.rssi).append(" dBm");
        t = new TextView(ctx);
        t.setTextColor(Color.WHITE);
        t.setText(s);
        rssitaskLayout.addView(t);
    }


}
